package sw6.visualschedule.entities;

import com.j256.ormlite.dao.ForeignCollection;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;

import sw6.visualschedule.R;
import tools.ThumbnailHelper;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Date;

@DatabaseTable(tableName = "activities")
public class ScheduleActivity implements Comparable<ScheduleActivity>, IVisualScheduleElement {

    public static final String ID_FIELD = "id";
    public static final String TITLE_FIELD = "title";
    public static final String IMAGE_FIELD = "image";
    public static final String SPONTANEOUS_FIELD = "spontaneous";
    public static final String NOTIFICATION_FIELD = "notification";
    
    //private static final String THUMBNAIL_DIR = Environment.getExternalStorageDirectory() + "/visualschedule/";
    
    /**
     * all persisted classes must define a no-argument constructor with at least package visibility
     */
    ScheduleActivity() { }
    
    /**
     * Creates a ScheduleActivity
     * @param title of the activity
     */
    public ScheduleActivity(final String title) {
        mTitle = title;
        
        //Default values
        mSpontaneous = false;
        mNotification = true;
    }
    /* 
     * Database Fields
     */
    
    @DatabaseField(columnName = ID_FIELD, generatedId = true)
    private transient Integer mId;
    
    @DatabaseField(columnName = TITLE_FIELD, canBeNull = false)
    private transient String mTitle;
    
    @ForeignCollectionField(eager = false)
    private transient ForeignCollection<Step> mSteps;
    
    @ForeignCollectionField(eager = false)
    private transient ForeignCollection<DbDate> mDates;
    
    @ForeignCollectionField(eager = false)
    private transient ForeignCollection<RepeatPattern> mRepeatPatterns;
    
    @DatabaseField(columnName = IMAGE_FIELD)
    private transient String mImage;
    
    @DatabaseField(columnName = SPONTANEOUS_FIELD, canBeNull = false)
    private transient Boolean mSpontaneous;
    
    @DatabaseField(columnName = NOTIFICATION_FIELD)
    private transient Boolean mNotification;
    
    private transient Date mInstanceDate;
    
    private transient ActivityStatusType mInstanceStatus;
    
    /**
     * Gets the ID
     * @return ID of activity
     */
    public Integer getId() {
        return mId;
    }
    
    /**
     * Gets the title
     */
    public String getTitle() {
        return mTitle;
    }
    
    /**
     * Sets the title
     */
    public void setTitle(final String title) {
        mTitle = title;
    }
    
    /**
     * Gets the address of the activity's image
     * @return address of image
     */
    public String getImage() {
        return mImage;
    }
    
    /**
     * Sets the address of the activity's image and creates a thumbnail. 
     */
    public void setImage(final String image) {
        if (mImage != null) {
            final File imageFile = new File(mImage);
            if (imageFile.exists()) {
                imageFile.delete();
            }
        }
        
        mImage = image;
        
        //Create thumbnail
        try {
			ThumbnailHelper.getThumbnailPath(image, 250, 250);
		} catch (FileNotFoundException e) {
			Log.e("ScheduleActivity", "setImage: " + e.getMessage());
		}
    }
    
    /**
     * Gets a thumbnail of the activity's image.
     * @param the context of the application.
     * @return the tumbnail image
     */
    public Bitmap getImageBitmap(final Context ctx) {
        Bitmap bitmap = null;
        
        if (mImage != null) {
            final File imageFile = new File(mImage);
            if (imageFile.exists()) {
                bitmap = BitmapFactory.decodeFile(mImage);
            }
        }
        
        //If the tumbnail creation fails, a default image is used instead.
        if (bitmap == null) {
            bitmap = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.default_image);
        }
        
        return bitmap;
    }
    
    /**
     * Gets a thumbnail of the activity's image.
     * @param the context of the application.
     * @param Max width
     * @param Max height
     * @return the tumbnail image
     */
    public Bitmap getImageBitmapThumbnail(final Context ctx, final int maxWidth, final int maxHeight) {
        Bitmap bitmap = ThumbnailHelper.getImageBitmapThumbnail(mImage, maxWidth, maxHeight);
        if (bitmap == null) {
            bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(ctx.getResources(), R.drawable.default_image), maxWidth, maxHeight, true);
        }
        
        return bitmap;
    }
    
    /**
     * Gets whether the activity spontaneous.
     * @return true if yes, false if not.
     */
    public Boolean isSpontaneous() {
        return mSpontaneous;
    }
    
    /**
     * Sets whether the activity should be spontaneous or not.
     * @param spontaneous
     */
    public void setSpontaneous(final Boolean spontaneous) {
    	mSpontaneous = spontaneous;
    }
    
    /**
     * Gets whether the activity uses notification or not.
     * @return true if it does, false if it doesn't
     */
    public Boolean useNotification() {
        return mNotification;
    }
    
    /**
     * Sets whether the activity should use notification.
     * @param true if it should, false if it shouldn't
     */
    public void setNotification(final Boolean notification) {
    	mNotification = notification;
    }
    
    /*
     * Steps
     */
    public ForeignCollection<Step> getSteps() {
        return mSteps;
    }
    
    /*
     * Repeat Patterns
     */
    public ForeignCollection<RepeatPattern> getRepeatPatterns() {
        return mRepeatPatterns;
    }
    
    /*
     * Dates
     */
    public ForeignCollection<DbDate> getDates() {
        return mDates;
    }
    
    /* Instance Date
     * 
     */
    public Date getInstanceDate() {
        return mInstanceDate;
    }
    
    public void setInstanceDate(final Date date) {
        mInstanceDate = date; 
    }
    
    /* Instance Status
     * 
     */
    public ActivityStatusType getInstanceStatus() {
        return mInstanceStatus;
    }
    
    public void setInstanceStatus(final ActivityStatusType status) {
        mInstanceStatus = status;
    }
    
    /*
     * Diverse
     */
    @Override
    public String toString() {
    	return getTitle();
    }

    @Override
    public int compareTo(final ScheduleActivity another) {
        return getInstanceDate().compareTo(another.getInstanceDate());
    }
    
    @Override
    public boolean equals(final Object obj) {
    	final ScheduleActivity compare = (ScheduleActivity) obj;
    	
    	boolean result = compare != null && compare.getId() == getId();
    	if (!result) {
    		return false;
    	}
    	
    	if (getInstanceDate() == null && compare.getInstanceDate() == null) {
    		return result;
    	} else if (getInstanceDate() == null || compare.getInstanceDate() == null) {
    		return false;
    	} else {
    		return result && compare.getInstanceDate().equals(getInstanceDate());
    	}
    }
}
